﻿package andy.demo.viewviewgroup;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import andy.demo.R;
public class ViewViewGoupTest extends Activity {

	private static final String TAG = "ViewViewGoupTest";
	private TextView tv_1;
	private TextView tv_2;
	private LinearLayout container;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.view_viewgoup);

		System.out.println("onCreate");
		tv_1 = (TextView) this.findViewById(R.id.tv_1);
		System.out.println("tv_1.getMeasuredWidth() = "
				+ tv_1.getMeasuredWidth());
		System.out.println("tv_1.getWidth() = " + tv_1.getWidth());
		System.out.println("tv_1.getLeft() = " + tv_1.getLeft());
		System.out.println("tv_1.getRight() = " + tv_1.getRight());
		System.out.println("tv_1.getTop() = " + tv_1.getTop());
		System.out.println("tv_1.getBottom() = " + tv_1.getBottom());

		tv_2 = (TextView) this.findViewById(R.id.tv_1);
		System.out.println("tv_2.getMeasuredWidth() = "
				+ tv_2.getMeasuredWidth());
		System.out.println("tv_2.getWidth() = " + tv_2.getWidth());
		System.out.println("tv_2.getLeft() = " + tv_2.getLeft());
		System.out.println("tv_2.getRight() = " + tv_2.getRight());
		System.out.println("tv_2.getTop() = " + tv_2.getTop());
		System.out.println("tv_2.getBottom() = " + tv_2.getBottom());

		System.out.println("----------------------------- ");
		tv_2.setWidth(100);
		System.out.println("tv_2.getWidth() = " + tv_2.getWidth());

		System.out.println("----------------------------- ");
		container = (LinearLayout) this.findViewById(R.id.code_container);
		TextView myText = new MyTextView(this);
		// myText.setLayoutParams(new
		// LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
		// LinearLayout.LayoutParams.FILL_PARENT));
		// container.setLayoutParams(new LayoutParams(200,200));
		// //因为视图已经解析到界面了，所以不可能再改变大小了
		// myText.setLayoutParams(new LayoutParams(100, 100));
		// myText.setBackgroundColor(Color.BLUE);
		// container.addView(myText);

		// myText.setBackgroundColor(Color.BLUE);
		// container.addView(myText, new LayoutParams(100, 100));

		myText.setLayoutParams(new LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT));
		myText.setBackgroundColor(Color.BLUE);
		container.addView(myText);

		//
		Button btn = (Button) this.findViewById(R.id.btn);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MyTextView textView = new MyTextView(ViewViewGoupTest.this);
				textView.setText("ss");
				container.addView(textView);
			}
		});

		Button btn_delete = (Button) this.findViewById(R.id.btn_delete);
		btn_delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				container.removeAllViews();
				System.out.println("removeAll ended");
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		System.out.println("onResume");
		System.out.println("tv_1.getMeasuredWidth() = "
				+ tv_1.getMeasuredWidth());
		System.out.println("tv_1.getWidth() = " + tv_1.getWidth());
		System.out.println("tv_1.getLeft() = " + tv_1.getLeft());
		System.out.println("tv_1.getRight() = " + tv_1.getRight());
		System.out.println("tv_1.getTop() = " + tv_1.getTop());
		System.out.println("tv_1.getBottom() = " + tv_1.getBottom());

		System.out.println("tv_2.getMeasuredWidth() = "
				+ tv_2.getMeasuredWidth());
		System.out.println("tv_2.getWidth() = " + tv_2.getWidth());
		System.out.println("tv_2.getLeft() = " + tv_2.getLeft());
		System.out.println("tv_2.getRight() = " + tv_2.getRight());
		System.out.println("tv_2.getTop() = " + tv_2.getTop());
		System.out.println("tv_2.getBottom() = " + tv_2.getBottom());

	}

//	@Override
//	public boolean dispatchKeyEvent(KeyEvent event) {
//		System.out.println("dispatchKeyEvent");
//		System.out.println("event.getRepeatCount() =" + event.getRepeatCount());
//		System.out.println("event.isLongPress() =" + event.isLongPress());
//		// if (event.getRepeatCount() > 0) {
//		// return true;
//		// }
//
//		if (event.getAction() == KeyEvent.ACTION_DOWN) {
//			System.out.println("a event.getRepeatCount() ="
//					+ event.getRepeatCount());
//			System.out
//					.println("a  event.isLongPress() =" + event.isLongPress());
//		}
//
//		return super.dispatchKeyEvent(event);
//		// return false;
//
//	};

	// @Override
	// public boolean onKeyDown(int keyCode, KeyEvent event) {
	//
	// if (keyCode== KeyEvent.KEYCODE_BACK) {
	// System.out.println("onKeyDown  back");
	// System.out.println("a event.getRepeatCount() =" +
	// event.getRepeatCount());
	// System.out.println("a  event.isLongPress() =" + event.isLongPress());
	// }
	// return super.onKeyDown(keyCode, event);
	//
	// }

	// @Override
	// public void onAttachedToWindow() {
	// // TODO Auto-generated method stub
	// super.onAttachedToWindow();
	//
	// System.out.println("parent onAttachedToWindow--------");
	// }

	@Override
	public void onDetachedFromWindow() {
		// TODO Auto-generated method stub
		super.onDetachedFromWindow();
		System.out.println("parent onDetachedFromWindow--------");
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub
		System.out.println("parent onWindowFocusChanged");
		super.onWindowFocusChanged(hasFocus);
	}

	private boolean catchHomeKey = true;

	@Override
	public void onAttachedToWindow() {
		if (catchHomeKey) {
			this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
		}
		super.onAttachedToWindow();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_HOME) {
			Log.e(TAG, "Home key down");
		}
		return super.onKeyDown(keyCode, event);
	}

}

class MyTextView extends TextView {

	public MyTextView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.setText("myText");
		this.requestFocusFromTouch();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);
		Log.i("Tag", "--------------");
		// View mView=getChildAt(0);
		// mView.measure(0, 0);

	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		Log.i("Tag", "width: " + getWidth() + ",height: " + getHeight());
		Log.i("Tag", "MeasuredWidth: " + getMeasuredWidth()
				+ ",MeasuredHeight: " + getMeasuredHeight());
	}

	@Override
	protected void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();

		System.out.println("myText onAttachedToWindow--------");
	}

	@Override
	protected void onDetachedFromWindow() {
		// TODO Auto-generated method stub
		super.onDetachedFromWindow();
		System.out.println("myText onDetachedFromWindow--------");
	}

	@Override
	protected void onFocusChanged(boolean focused, int direction,
			Rect previouslyFocusedRect) {
		System.out.println("child onFocusChanged");
		System.out.println("focused =" + focused);
		System.out.println("direction =" + direction);
		System.out.println("previouslyFocusedRect =" + previouslyFocusedRect);

		super.onFocusChanged(focused, direction, previouslyFocusedRect);

	};

	@Override
	protected void onFinishInflate() {
		System.out.println("child  onFinishInflate--");
		super.onFinishInflate();

	};

}
